package org.zlex.chapter03;


import java.security.*;
import java.security.spec.*;

/**
 * @author : wushikai
 * <p>
 * date : 2021-04-22
 */
public class KeySpecDemo {



    public  static void genKey ( ) throws  Exception {

        String rsa = "RSA";
        KeyPairGenerator keygen = KeyPairGenerator.getInstance(rsa);
        keygen.initialize(1024);


        KeyPair keyPair = keygen.genKeyPair();

        byte[] publicKeyBytes = keyPair.getPublic().getEncoded();


        //生成公钥
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance(rsa);

        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        //生成私钥
        byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();

        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);

        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);



    }



    public static void main(String[] args) throws Exception {
        genKey();

    }


}
